/**
 * 请求拦截
 * @param {Object} vm - Vue 实例
 */
module.exports = (vm) => {
    uni.$u.http.interceptors.request.use(
        async (config) => {
            console.log('请求拦截', config);

            // 确保 config.data 不为 undefined，赋予默认值
            config.data = config.data || {};

             // 从本地存储获取 token
             const token = uni.getStorageSync('token');
             const timeStamp = uni.getStorageSync('timeStamp')
             const nonceStr = uni.getStorageSync('nonceStr')
             const paySign = uni.getStorageSync('paySign')
             const packageVal = uni.getStorageSync('packageVal')
             const signType = uni.getStorageSync('signType')
            // 初始化请求头
            config.header = config.header || {};

            config.header['Authorization'] = `Bearer ${token}`;
            // 确保 Content-Type 为 application/json
            // if (!config.header['Content-Type']) {
            //     config.header['Content-Type'] = 'application/json';
            // }

            // 
            if (config.url.includes('/face/fusion' || '/task')) {
                config.header['Content-Type'] = 'application/x-www-form-urlencoded';
            }

            if (timeStamp) {
                config.header['Wechatpay-Timestamp'] = timeStamp;
            }

            if (nonceStr) {
                config.header['Wechatpay-Nonce'] = nonceStr;
            }

            if (paySign) {
                config.header['Wechatpay-Signature'] = paySign;
            }

            if (packageVal) {
                config.header['Wechatpay-Serial'] = packageVal;
            }

            if (signType) {
                config.header['Wechatpay-Signature-Type'] = signType;
            }
            // 输出最终请求配置以便调试
            console.log('最终请求配置:', config);

            return config;
        },
        (error) => {
            console.error('请求拦截错误:', error);
            return Promise.reject(error);
        }
    );
};